﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using KeepAnimals.BLL;
using KeepAnimals.UI;
namespace KeepAnimals.UI
{
    public partial class Home : Form
    {
        private int _idDox;
        private int _idAnimal;
        private int _idDepartment;

        public int IdDepartment
        {
            get { return _idDepartment; }
            set { _idDepartment = value; }
        } 

        public int IdAnimal
        {
            get { return _idAnimal; }
            set { _idAnimal = value; }
        }
        private int IdBox
        {
            get { return _idDox; }
            set { _idDox = value; }
        }
        public Home()
        {
            InitializeComponent();
        }

        private void btn_ViewDepartments_Click(object sender, EventArgs e)
        {
            var departments = Department.GetListOfAllDepartments();
            panel_ContentDepartments.Controls.Clear();
            LoadDepartments(departments, panel_ContentDepartments);
        }
        public void LoadDepartments(List<Department> departments, Panel panel)
        {
            int x = 0;
            int y = 0;
            const int NBOBJECTLIGNE = 4; //nombre d'object par ligne
            //const int WIDTHOBJECT = 50;
            int mainContainWidth = panel.Width;
            int taille = mainContainWidth / NBOBJECTLIGNE;


            double nbObjectTotal = departments.Count; // nombre objet dans la base
            int compteurObject = 1;
            double result = nbObjectTotal / NBOBJECTLIGNE;
            int nbLigne = roundUp(result);

            for (int i = 0; i < nbLigne; i++)
            {


                for (int j = 1; j <= NBOBJECTLIGNE; j++)
                {
                    foreach (var department in departments)
                    {
                        if (j <= NBOBJECTLIGNE && compteurObject <= nbObjectTotal)
                        {
                            j++;

                            
                            var pix = new Button();

                            pix.Text = "Département " + compteurObject;
                            pix.TextAlign = System.Drawing.ContentAlignment.TopCenter;
                            pix.BackgroundImage = KeepAnimals.UI.Properties.Resources.ImgDepartment1;
                            pix.BackgroundImageLayout = ImageLayout.Stretch;

                            //pix.BackColor = Color.Red;
                            pix.Width = taille;
                            pix.Height = taille;
                            pix.Left = x;
                            pix.Top = y;

                            x += taille;
                            string id = department.Id.ToString();
                            panel.Controls.Add(pix);
                            pix.Name = department.Id.ToString();
                            pix.Click += ViewBoxOfDepartment;

                            compteurObject++;

                        }
                        else
                        {
                            continue;
                        }
                    }
                }
                x = 0;
                y += taille;
            }
        }

        private void ViewBoxOfDepartment(object sender, EventArgs e)
        {
            var dep = (Button)sender;
            var iddep = dep.Name;
            int idDepartment = Int32.Parse(iddep);
            _idDepartment = idDepartment;
            var box = Box.GetBoxsByIdDepartment(idDepartment);
            panel_ContentBox.Controls.Clear();
            LoadBox(box, panel_ContentBox);
        }
        public void LoadBox(List<Box> boxes, Panel panel)
        {
            int x = 0;
            int y = 0;
            const int NBOBJECTLIGNE = 4; //nombre d'object par ligne
            //const int WIDTHOBJECT = 50;
            int mainContainWidth = panel.Width;
            int taille = mainContainWidth / NBOBJECTLIGNE;


            double nbObjectTotal = boxes.Count; // nombre objet dans la base
            
            double result = nbObjectTotal / NBOBJECTLIGNE;
            int nbLigne = roundUp(result);

            for (int i = 0; i < nbLigne; i++)
            {

                for (int j = 1; j <= NBOBJECTLIGNE; j++)
                {
                    int compteurObject = 1;
                    foreach (var boxe in boxes)
                    {
                        if (j <= NBOBJECTLIGNE && compteurObject <= nbObjectTotal)
                        {
                            j++;

                            
                            var pix = new Button();

                            //pix.Text = compteurObject.ToString();
                            pix.Name = boxe.Id.ToString();


                            //pix.BackColor = Color.Red;
                            pix.Width = taille;
                            pix.Height = taille;
                            pix.Left = x;
                            pix.Top = y;

                            x += taille;
                            string id = boxe.Id.ToString();

                            pix.BackgroundImage = KeepAnimals.UI.Properties.Resources.ImgCageVide;

                            if (boxe.Empty == Box.BOX_FULL)
                            {
                                pix.BackgroundImage = KeepAnimals.UI.Properties.Resources.ImgCageFull;
                            }
                            pix.BackgroundImageLayout = ImageLayout.Stretch;

                            panel.Controls.Add(pix);
                            pix.Click += ViewInfoAnimal;

                            //compteurObject++;

                        }


                        else
                        {
                            break;
                        }
                    }
                }
                x = 0;
                y += taille;
            }
        }

        private void ViewInfoAnimal(object sender, EventArgs e)
        {
            var button = (Button)sender;
            int idButton = Convert.ToInt32(button.Name.ToString());
            _idDox = idButton;
            var box = Box.GetBoxById(idButton);
            //MessageBox.Show(box.IdAnimal.ToString());
            //MessageBox.Show(idButton.ToString());
            //MessageBox.Show(box.Empty.ToString());
            if (box.Empty == Box.BOX_EMPTY)
            {
                //MessageBox.Show("vide");
                FormAddAnimal formulaire = new FormAddAnimal(IdBox, IdDepartment);
                formulaire.Show();
            }
            else
            {
                Button PrintRapport = new Button();
                PrintRapport.FlatAppearance.BorderSize = 0;
                PrintRapport.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
                PrintRapport.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                //PrintRapport.Location = new System.Drawing.Point(65, 59);
                PrintRapport.Name = "btn_GenerateReport";
                PrintRapport.Size = new System.Drawing.Size(200, 23);
                PrintRapport.TabIndex = 0;
                PrintRapport.Text = "Générer un rapport";
                PrintRapport.UseVisualStyleBackColor = true;
                PrintRapport.Click += GenerateReport;
                panelRapport.Controls.Clear();

                panelRapport.Controls.Add(PrintRapport);
                var animal = Animal.GetAnimalByIdBox(idButton);
                _idAnimal = animal.Id;
                if (animal.IdCategory == Animal.ANIMAL_CAT)
                {
                    //PictureBoxImgAnimal.BackgroundImage = KeepAnimals.UI.Properties.Resources.ImgCat1;
                }
                else
                {
                    //PictureBoxImgAnimal.BackgroundImage = KeepAnimals.UI.Properties.Resources.ImgDog1;
                }
                //PictureBoxImgAnimal.BackgroundImageLayout = ImageLayout.Stretch;

                textBoxName.Text = animal.Name;
                textBoxAge.Text = animal.Age.ToString();
                textBoxWeight.Text = animal.Weight.ToString();
                textBoxState.Text = animal.State;
                textBoxDateA.Text = animal.DateArrival.ToString();
                textBoxDateD.Text = animal.DateDeparture.ToString();
                textBoxDescription.Text = animal.Description;

                Label nomAnimal = new Label();
                nomAnimal.Text = "Nom : " + animal.Name;
                Label ageAnimal = new Label();
                ageAnimal.Text = animal.Age.ToString() + " an(s)";
                Label poidsAnimal = new Label();
                poidsAnimal.Text = "Poids : " + animal.Weight.ToString();
                Label santeAnimal = new Label();
                santeAnimal.Text = "Santé : " + animal.State;
                Label desciptionAnimal = new Label();
                desciptionAnimal.Text = "Description : " + animal.Description;



            }

        }

        private void btn_UpdateAnimal_Click(object sender, EventArgs e)
        {
            var id = IdAnimal;
            Animal animal = AnimalFactory.CreateAnimal(Animal.ANIMAL_DOG); ;
            animal.Id = id;
            animal.Name = textBoxName.Text;
            animal.Age = Int32.Parse(textBoxAge.Text);
            animal.Weight = Double.Parse(textBoxWeight.Text);
            animal.State = textBoxState.Text;
            animal.Description = textBoxDescription.Text;
            animal.UpdateAnimalInDatabase();
            MessageBox.Show("Les mises à jours ont bien été prises en compte.");

        }


        private void GenerateReport(object sender, EventArgs e)
        {
            
        }
        static public void Affiche(string req)
        {
            MessageBox.Show(req);
        }

        private void btn_DeleteAnimal_Click(object sender, EventArgs e)
        {
            var id = IdBox;
            Animal.DeleteAnimalById(IdBox);
            Box box = new Box();
            box.Id = id;
            box.IdAnimal = 0;
            box.Empty = Box.BOX_EMPTY;
            box.UpdateIDAnimalInDatabaseWithIdBox();
            MessageBox.Show("L'animal à bien été retiré de la base.");
            
            btn_DeleteAnimal.Name = IdDepartment.ToString();
            //ViewBoxOfDepartment();
        }

        public int roundUp(double nombre)
        {
            int res = 0;

            if (Convert.ToInt32(nombre) == nombre)
            {
                res = Convert.ToInt32(nombre);
            }
            else
            {
                var nb = Math.Floor(nombre);
                res = Convert.ToInt32(nb) + 1;
            }

            return res;
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            //BOUTON EFFACE
            panelContentTabAnimal.Controls.Clear();
            panel_ContentDepartments.Controls.Clear();
            //panel_ContentBox.Controls.Clear();
            panelRapport.Controls.Clear();
            labelNameBloc1.Text = "Text";
            labelNameBloc2.Text = "Text";
            labelNameBloc3.Text = "Text";


        }

        private void panel2_Click(object sender, EventArgs e)
        {
            Home home = new Home();
            home.Show();
        }

        private void Home_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            panelContentTabAnimal.Controls.Clear();
            panel_ContentDepartments.Controls.Clear();
            //panel_ContentBox.Controls.Clear();
            panelRapport.Controls.Clear();
            labelNameBloc1.Text = "Liste des animaux";
            //labelNameBloc2.Text = "Text";
            //labelNameBloc3.Text = "Text";

            var boxes = Box.GetListOfAllBox();
            panel_ContentBox.Controls.Clear();
            LoadBox(boxes, panel_ContentBox);
        }

        private void btn_FomularAdoption_Click(object sender, EventArgs e)
        {
            FormAdoptAnimal adoptanimal = new FormAdoptAnimal();
            adoptanimal.Show();
        }

        

        
    }
}
